package com.gogowan.agroenterprise.widget.adapter;

import java.util.List;

import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.utils.INewAdapterClick;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HwScanAdapter extends BaseAdapter {

	private final INewAdapterClick click;
	private Context context;
	private List<String> datas;

	public HwScanAdapter(Context context, List<String> datas, INewAdapterClick click) {
		this.context = context;
		this.datas = datas;
		this.click=click;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.hw_scan_item, null);
			viewHolder = new ViewHolder();
			viewHolder.txtCode = (TextView) convertView.findViewById(R.id.txtCode);
			viewHolder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);
			viewHolder.tv_no = (TextView) convertView.findViewById(R.id.tv_no);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		String str = datas.get(position);
		viewHolder.txtCode.setText(str);
		viewHolder.tv_no.setText((datas.size()-position)+"");
		viewHolder.iv_delete.setOnClickListener(view -> {
			click.onItemClick(view,position);
		});
		return convertView;
	}

	public void setList(List<String> codeStringList) {
		datas=codeStringList;
	}

	static class ViewHolder {
		public TextView tv_no;
		TextView txtCode;
		ImageView iv_delete;
	}

}
